package com.acme.rogelio;

import com.acme.entity.Empleado;
import java.awt.Color;
import java.awt.Font;
import java.util.HashMap;
import javax.swing.JComponent;
import javax.swing.text.JTextComponent;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.xswingx.PromptSupport;

public class SubPanelEmpleado extends javax.swing.JPanel {

        public SubPanelEmpleado() {
        initComponents();
        establecerMensajes();
    }

       @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        bean = new com.acme.entity.Empleado();
        cmpnumero = new javax.swing.JTextField();
        cmpNombre = new javax.swing.JTextField();
        cmpApellidos = new javax.swing.JTextField();
        cmpTelefono = new javax.swing.JTextField();
        cmpEmail = new javax.swing.JTextField();

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, bean, org.jdesktop.beansbinding.ELProperty.create("${numero}"), cmpnumero, org.jdesktop.beansbinding.BeanProperty.create("text"), "cmpnumero");
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, bean, org.jdesktop.beansbinding.ELProperty.create("${contacto.nombre}"), cmpNombre, org.jdesktop.beansbinding.BeanProperty.create("text"), "cmpNombre");
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, bean, org.jdesktop.beansbinding.ELProperty.create("${contacto.apellidos}"), cmpApellidos, org.jdesktop.beansbinding.BeanProperty.create("text"), "cmpApellidos");
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, bean, org.jdesktop.beansbinding.ELProperty.create("${contacto.telefono}"), cmpTelefono, org.jdesktop.beansbinding.BeanProperty.create("text"), "cmpTelefono");
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, bean, org.jdesktop.beansbinding.ELProperty.create("${contacto.email}"), cmpEmail, org.jdesktop.beansbinding.BeanProperty.create("text"), "cmpEmail");
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmpNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmpnumero, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44)
                        .addComponent(cmpEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE))
                    .addComponent(cmpApellidos, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                    .addComponent(cmpTelefono, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmpnumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmpEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmpNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmpApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmpTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.acme.entity.Empleado bean;
    private javax.swing.JTextField cmpApellidos;
    private javax.swing.JTextField cmpEmail;
    private javax.swing.JTextField cmpNombre;
    private javax.swing.JTextField cmpTelefono;
    private javax.swing.JTextField cmpnumero;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public Empleado getBean() {
        return bean;
    }

     public void setBean(Empleado bean) {
        if (bean != null) {
            for (Binding bin : bindingGroup.getBindings()) {
                bin.setSourceObject(bean);

            }
            this.bean = bean;
            bindingGroup.bind();
            cmpEmail.requestFocus();
        }

    }
    private final static HashMap<String, String> mapa = new HashMap<String, String>();

    static {
        mapa.put("cmpnumero", "<numero>");
        mapa.put("cmpEmail", "<Mail>");
        mapa.put("cmpNombre", "<Nombre>");
        mapa.put("cmpApellidos", "<Apellidos>");
        mapa.put("cmpTelefono", "<Telefono>");
    }

    private void establecerMensajes() {
        for (Binding bin : bindingGroup.getBindings()) {
            Object obj = bin.getTargetObject();
            JTextComponent cmp = null;
            if (obj instanceof JTextComponent ) {
                cmp = (JTextComponent) obj;
            }
            if (cmp != null) {
                String mensaje = mapa.get(bin.getName());
                PromptSupport.setFontStyle(Font.ITALIC, cmp);
                PromptSupport.setForeground(Color.BLUE, cmp);
                PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, cmp);
                PromptSupport.setPrompt(mensaje, cmp);
            }

        }


    }

    @Override
    public void setEnabled(boolean enabled) {
        for (Binding bin : bindingGroup.getBindings()) {
            JComponent cmp = (JComponent) bin.getTargetObject();
            cmp.setEnabled(enabled);
        }
    }

    public void activarCampoNuemro(boolean v){
        cmpnumero.setEnabled(v);
    }

}
